﻿using System.IO;
using ZilLion.Service.Core.ConfigManager.Manager;
using ZilLion.Service.Core.MongoDbManager;
using ZilLion.Service.Core.MongoDbManager.GridFs;
using ZilLion.Service.TaskPlatform.Domain.Respository.Interface;

namespace ZilLion.Service.TaskPlatform.Domain.Respository.GridFs
{
    public class MongoTaskPackageRepository : GridFsRepository, ITaskPackageRespository
    {
        public MongoTaskPackageRepository() : base(new DefaultMongoDbContext
        {
            DatabaseName = "TaskManager",
            RootName = "Package",
            ServerConnectString = ZilLionConfigurationManager.GetConfigInstance<TaskManagerServiceConfig>()
                .PackageServerConn
        })
        {
        }

        public byte[] GetPackageBytes(string packageKey)
        {
            return GetFileAsBytesByFileName(packageKey);
        }

        public string UploadFileToGridFs(Stream fileStream, string packageKey)
        {
            var mongoGridFsFileInfo = UploadFile(fileStream, packageKey);
            return mongoGridFsFileInfo.MD5.ToUpper();
        }
    }
}